import { Component, OnInit } from '@angular/core';
import { Response } from '@angular/http';
import {AuthService} from "../../auth.service";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {StaticValidators} from "../../../common/validators";
import {Router} from "@angular/router";

@Component({
  selector: 'app-account-login',
  templateUrl: './account-login.component.html',
  styleUrls: ['./account-login.component.less']
})
export class AccountLoginComponent implements OnInit {
  signinForm: FormGroup = new FormGroup({
    'userData': new FormGroup({
      'userName': new FormControl(
        'liuqiang', [Validators.required, this.checkUserNameStrength.bind(this)]),
      'password': new FormControl('123qwe', [Validators.required, this.onCheckPassword.bind(this)])
    })
  });
  constructor(private authService: AuthService, private router: Router) { }

  ngOnInit() {}
  onLogin() {
    this.authService.login(this.signinForm.value.userData).subscribe(
      (data) => {
        if (data.code === 1) {
          this.router.navigate(['/']);
        }else {
          confirm(data.msg);
        }
      }
    );
  }
  checkUserNameStrength(control: FormControl) {
    return StaticValidators.userNameStrength(control);
  }
  onCheckPassword(control: FormControl) {
    return StaticValidators.checkPassword(control);
  }
}
